package com.jprocms.module.cms.controller.portal.page;

import com.jpro.module.system.api.user.AdminUserApi;
import com.jprocms.module.cms.dal.dataobject.globalconfig.GlobalConfigDO;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.enums.ErrorCodePortalConstants;
import com.jprocms.module.cms.exception.PortalServiceExceptionUtil;
import com.jprocms.module.cms.service.content.ContentCountService;
import com.jprocms.module.cms.service.globalconfig.GlobalConfigService;
import com.jprocms.module.cms.service.site.SiteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 会员页面相关 Controller
 *
 * @author jprocms
 */
@Controller
public class MemberController {

    //登录页面
    public static final String TPL_LOGIN = "member/login";
    //注册页面
    private static final String TPL_REGISTER = "member/register";
    //修改密码页面
    private static final String TPL_PASSWORD_UPDATE = "member/user_password_reset";
    //密码重置页面
    private static final String TPL_PASSWORD_RESET = "member/password_reset";
    //个人信息展示页面
    private static final String TPL_USER_CENTER = "member/user";
    //修改头像模板
    private static final String TPL_USER_AVATAR = "member/user_avatar";
    //修改个人信息模板
    private static final String TPL_USER_ACCOUNT = "member/user_account";

    private final SiteService siteService;

    private final GlobalConfigService globalConfigService;

    private final AdminUserApi adminUserApi;
    private final ContentCountService contentCountService;

    public MemberController(SiteService siteService, GlobalConfigService globalConfigService, AdminUserApi adminUserApi, ContentCountService contentCountService) {
        this.siteService = siteService;
        this.globalConfigService = globalConfigService;
        this.adminUserApi = adminUserApi;
        this.contentCountService = contentCountService;
    }


    @GetMapping({"/login", "/{sitePath:[\\w-]+}/login"})
    public String login(@PathVariable(required = false) String sitePath, HttpServletRequest request) {
        SiteDO site = siteService.getSite(sitePath);
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_LOGIN);
    }

    @GetMapping({"/register", "/{sitePath:[\\w-]+}/register"})
    public String register(@PathVariable(required = false) String sitePath, HttpServletRequest request) {
        SiteDO site = siteService.getSite(sitePath);
        GlobalConfigDO globalConfig = globalConfigService.getDefGlobalConfig();
        if (!globalConfig.getRegisterConfig().isEnabled()) {
            throw PortalServiceExceptionUtil.exception(ErrorCodePortalConstants.REGISTER_CLOSE);
        }
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_REGISTER);
    }

    /**
     * 会员中心修改密码页面
     * @param sitePath 站点访问目录
     * @return
     */
    @GetMapping({"/password-reset", "/{sitePath:[\\w-]+}/password-reset"})
    public String updatePassword(@PathVariable(required = false) String sitePath) {
        SiteDO site = siteService.getSite(sitePath);
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_PASSWORD_UPDATE);
    }

    /**
     * 登录页面的重置密码
     * @param sitePath 站点访问目录
     * @return
     */
    @GetMapping({"/pwd-reset", "/{sitePath:[\\w-]+}/pwd-reset"})
    public String passwordReset(@PathVariable(required = false) String sitePath) {
        SiteDO site = siteService.getSite(sitePath);
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_PASSWORD_RESET);
    }

    @GetMapping({"/user", "/{sitePath:[\\w-]+}/user"})
    public String userCenter(@PathVariable(required = false) String sitePath, Map<String, Object> modelMap, HttpServletRequest request) {
        SiteDO site = siteService.getSite(sitePath);
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_USER_CENTER);
    }

    @GetMapping({"/avatar", "/{sitePath:[\\w-]+}/avatar"})
    public String avatar(@PathVariable(required = false) String sitePath, HttpServletRequest request) {
        SiteDO site = siteService.getSite(sitePath);
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_USER_AVATAR);
    }

    @GetMapping({"/account", "/{sitePath:[\\w-]+}/account"})
    public String account(@PathVariable(required = false) String sitePath, HttpServletRequest request) {
        SiteDO site = siteService.getSite(sitePath);
        checkMemberClose();
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_USER_ACCOUNT);
    }

    private void checkMemberClose() {
        GlobalConfigDO globalConfig = globalConfigService.getDefGlobalConfig();
        if (!globalConfig.getOtherConfig().getMemberEnabled()) {
            throw PortalServiceExceptionUtil.exception(ErrorCodePortalConstants.MEMBER_CLOSE);
        }
    }

}
